package com.xbongbong.paas.controller.workflow;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.service.ManualSignService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.manual.dto.ReuseSignatureAddDTO;
import com.xbongbong.pro.manual.dto.ReuseSignatureUpdateDTO;
import com.xbongbong.pro.manual.vo.ReuseSignatureGetVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author sunshaoxiang
 * @ClassName ManualSignController
 * @Description 手签 controller层
 * @Date 2022/8/11 10:08
 * @Version 1.0
 **/
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/manual/sign")
public class ManualSignController {

    public static final Logger LOG = LoggerFactory.getLogger(ManualSignController.class);

    @Resource
    private ManualSignService manualSignService;

    /**
     * 获取可复用的历史手签
     *
     * @return 签名信息
     */
    @RequestMapping(value = "/get/reuseSignature", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getReuseSignature(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) throws Exception {
        XbbResponse<ReuseSignatureGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ReuseSignatureGetVO reuseSignatureGetVO = manualSignService.getReuseSignature(baseDTO);
                response = new XbbResponse<>(reuseSignatureGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 更新可复用的历史手签
     *
     * @return 签名信息
     */
    @RequestMapping(value = "/update/reuseSignature", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateReuseSignature(@RequestBody @Valid ReuseSignatureUpdateDTO reuseSignatureUpdateDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = manualSignService.updateReuseSignature(reuseSignatureUpdateDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 新增可复用的历史手签
     *
     * @return 签名信息
     */
    @RequestMapping(value = "/add/reuseSignature", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addReuseSignature(@RequestBody @Valid ReuseSignatureAddDTO reuseSignatureAddDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = manualSignService.addReuseSignature(reuseSignatureAddDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 删除可复用的历史手签
     *
     * @return 签名信息
     */
    @RequestMapping(value = "/remove/reuseSignature", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String removeReuseSignature(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = manualSignService.removeReuseSignature(baseDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
